package com.jatec.webcopy;

import net.bgreco.DirectoryPicker;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;


public class WebcopyActivity extends Activity {
	
	static final int DIALOG_ADD_NEW = 0;
	static final int DIALOG_PROPERTIES = 1;
	
	private Context myContext;
	
	private WebItemManager manager;
	
	private LinearLayout main;
	private ListView listview;
	private Dialog addNewDialog;
	
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        myContext = this;
        
        manager = WebItemManager.instance();
        
        setContentView(R.layout.jatec_main);
        
        initView(savedInstanceState);
        
    }
    
    private void initView(Bundle bundle){
        main = (LinearLayout) findViewById(R.id.rootLayout);

        listview = (ListView) findViewById(R.id.listView);     
        registerForContextMenu(listview);        
        //ArrayAdapter<WebItem> adapter = new ArrayAdapter<WebItem>(this, R.layout.list_item, db.readAll());
        listview.setAdapter(manager);
        
        Button add = (Button) findViewById(R.id.add_button);
        add.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				addNew();
			}
		});
        
        Button reload = (Button) findViewById(R.id.reload_button);
        reload.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				reloadAll();
			}
		});  
        
    
    }
    
    private void initDialog(){
    	addNewDialog = new Dialog(myContext);

    	addNewDialog.setContentView(R.layout.jatec_add_new_dialog);
    	addNewDialog.setTitle("Add New..."); 
    	
    	
        Button dir = (Button) addNewDialog.findViewById(R.id.dir_button);
        dir.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
			}
		});
    	
        Button ok = (Button) addNewDialog.findViewById(R.id.ok_button);
        ok.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				EditText text = (EditText) addNewDialog.findViewById(R.id.name);
				String name = text.getText().toString();
				
				text = (EditText) addNewDialog.findViewById(R.id.url);
				String url = text.getText().toString();
				
				WebItem item = new WebItem();
				item.setName(name);
				item.setUrl(url);
				
				manager.add(item);
				addNewDialog.dismiss();
			}
		});
        
        Button cancel = (Button) addNewDialog.findViewById(R.id.cancel_button);
        cancel.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				addNewDialog.dismiss();
			}
		});    
    }
    
    
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		
		if (v.getId()==R.id.listView) {
		    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
		    menu.setHeaderTitle(manager.get(info.position).getName());	    
		    MenuInflater inflater = getMenuInflater();
		    inflater.inflate(R.menu.context_menu, menu);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.options_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {	
		if( item.getItemId() == R.id.exit_menu){
			this.finish();		
		}
		else{
			main.setBackgroundColor(Color.DKGRAY);
		}
		return true;
	}
	
	
		
	@Override
	protected Dialog onCreateDialog(int id) {
		switch(id){
		case DIALOG_ADD_NEW:
			if( addNewDialog == null ){
				initDialog();
			}
			return addNewDialog;
		}
		return super.onCreateDialog(id);
	}
	
	

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
		WebItem web = manager.get(info.position);
		if( web != null ){
			if( item.getItemId() == R.id.open_menu ){
				
				return true;
			}
			else if( item.getItemId() == R.id.reload_menu ){
				
				try{
					String[] args = new String[]{"-v", "-f", "-d", "1", "-p", web.getLocalPath(), web.getUrl()}; 				
					new WebCopy().compat(args);
				}
				catch(Exception e){
					e.printStackTrace();
				}
				
				return true;
			}
			else if( item.getItemId() == R.id.delete_menu ){
				manager.remove(web);
				return true;
			}
		}
		return false;
	}

	protected void addNew(){
		//main.setBackgroundColor(Color.RED);
		Intent intent = new Intent(this, AddNewActivity.class);
		startActivityForResult(intent, 0);
	}
	
	protected void reloadAll(){
//		//main.setBackgroundColor(Color.GREEN);
//		Intent intent = new Intent(this, DirectoryPicker.class);
//		// optionally set options here
//		intent.putExtra(DirectoryPicker.ONLY_DIRS, true);
//		intent.putExtra(DirectoryPicker.START_DIR, "/");
//		startActivityForResult(intent, DirectoryPicker.PICK_DIRECTORY);
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(requestCode == DirectoryPicker.PICK_DIRECTORY && resultCode == RESULT_OK) {
			Bundle extras = data.getExtras();
			String path = (String) extras.get(DirectoryPicker.CHOSEN_DIRECTORY);
			// do stuff with path
			System.out.println("selected : " + path);
		}
	}
}